package io.github.uicdb.badskill.network;

import com.mojang.logging.LogUtils;
import io.github.uicdb.badskill.*;
import lombok.RequiredArgsConstructor;
import net.minecraft.client.Minecraft;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.resources.ResourceLocation;
import net.minecraftforge.fml.LogicalSide;
import net.minecraftforge.network.NetworkEvent;
import org.slf4j.Logger;

import java.util.function.Supplier;
@RequiredArgsConstructor
public class LearnResultPackage {
    private final ResourceLocation id;
    public static LearnResultPackage create(ResourceLocation resourceLocation){
        return new LearnResultPackage(resourceLocation);
    }    private static final Logger LOGGER= LogUtils.getLogger();
    public  void toBytes(FriendlyByteBuf buf) {
        buf.writeResourceLocation(id);
    }

    public static LearnResultPackage create(FriendlyByteBuf buf) {
        return create(buf.readResourceLocation());
    }

    public  void handle(Supplier<NetworkEvent.Context> contextSupplier) {
        var context=contextSupplier.get();
        context.enqueueWork(() -> {
            if(context.getDirection().getReceptionSide()== LogicalSide.CLIENT){
                    Minecraft.getInstance().player.getCapability(PlayerSkillCapabilityProvider.CAPABILITY).ifPresent(capability -> {
                        SkillOption option = SkillOptionManager.INSTANCE.OPTIONS.get(id);
                        if (option != null) {
                            capability.options.put(id,option);
                        }
                    });
            }
        });
        context.setPacketHandled(true);
    }
}
